<?php
  require_once 'lib/HTML/Util.php';
  require_once 'lib/HTML/LinkLoader.php';
  require_once 'lib/HTML/FormElements.php';  
  require_once 'controllers/Track.php';

  if(is_numeric($_GET['tid']) && $_GET['tid'] > 0)
  {
  	    
  $track = new Track(array(id=>$_GET['tid']));
  try{ $track->getDetails(); }
  catch(MTException $e){ die($e->getMessage()); }

  if($track->result() == null)
      header("Location: ?view=track&action=listen&error=not+found");
      
  $track->getCoverArt();
  //$link = $track->getLink();
  //echo("link-->".$link);
  //print_r($track->result());
  //the single track informations
  $smarty->assign_by_ref("track_info",$track->result());
  $smarty->assign('time_added', $track->getTime());
  $smarty->assign("title", "{$track->result()->title}, by {$track->result()->artist}");
  
  }
  else{ 
    if($_GET['view'] != 'playlist'){
      header("Location: index.php?error");
    }
  }
    
  $ll = new LinkLoader();
  $ll->loadCSS(array('listen.css','tabs.css','form.css'));
  
  $ll->loadJs(array('Ajax.js','WebServices/WebService.js',
                    'WebServices/MusicBrainz.js','Components/Track.js',
                    'Components/Article.js','MusicTalk.js','Tabs.js','WebServices/YouTubeVids.js','lib/swfobject.js','Player.js'));
  $ll->loadJs('js.js','views/track/listen/');
  
  $smarty->assign_by_ref("ll", $ll);
  $smarty->assign_by_ref("html",new Util());
  $smarty->assign_by_ref("fe",new FormElements());
  
  /*
   * When on playlist mode it
   * will send only the updated part of the page
   * for each different track
   */
  if(isset($_POST['update_track'])){
  	include_once "lib/WebServices/Proxy.php";
    Proxy::send('{id:'.$track->result()->id.',title:\''.addslashes($track->result()->title).'\',artist:\''.addslashes($track->result()->artist).'\', release:\''.addslashes($track->result()->release_title).'\',fav_user:'.($track->result()->fav_user?$track->result()->fav_user:'null').'}' .
      '<!--START-->'.
      $smarty->fetch("../track/listen/track_info.tpl").'<!--SEP-->'.$smarty->fetch("../track/listen/view.tpl"),
      'html',
      60
    );
    exit();
  }
     
  $smarty->display("head.tpl");
  $smarty->display("nav.tpl");
  // main content
  echo '<div id="track_info">';
  $smarty->display("../track/listen/track_info.tpl");
  echo '</div>';
  $smarty->display("../track/listen/player.tpl");
  echo '<div id="track_view">';
  $smarty->display("../track/listen/view.tpl");
  echo '</div>';
  $smarty->display("footer.tpl");
?>

    
    
